//
//  HomeViewController.m
//  Wy_BuyBooks
//
//  Created by wangyang on 2020/2/6.
//  Copyright © 2020 wangyang. All rights reserved.
//

#import "HomeViewController.h"
#import "WiredVc.h"
#import "BiographyVc.h"
#import "CelebrityinkVc.h"
#import "channelVc.h"
#import "TopVc.h"
@interface HomeViewController ()<SGSegmentedControlDelegate,UIScrollViewDelegate>

@property (nonatomic,strong)   SGSegmentedControl   *WySegmentedControl;
@property (nonatomic,strong)   UIScrollView         *WyScrollView;
@property (nonatomic,strong)   NSArray              *WyArray;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initSetUI) name:@"initSetUI" object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iniSetChild) name:@"iniSetChild" object:nil];
    [self initSetUI];
    [self iniSetChild];

      
}

-(void)initSetUI
{
   
    _WyArray = @[@"线装古籍",@"人物传记",@"名人墨迹",@"新书频道",@"好书排行"];
    _WyScrollView = [[UIScrollView alloc]init];
    [self.view addSubview:_WyScrollView];
    _WyScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, WySCREENHEIGHT);
    _WyScrollView.contentSize = CGSizeMake(self.view.frame.size.width*_WyArray.count, 0);
    _WyScrollView.pagingEnabled = YES;
    _WyScrollView.bounces = NO;
    _WyScrollView.showsVerticalScrollIndicator = NO;
    _WyScrollView.delegate = self;
    _WyScrollView.backgroundColor = [UIColor clearColor];
    
    
    WiredVc  *Delivered = [[WiredVc alloc]init];
    [self.WyScrollView addSubview:Delivered.view];
//    Delivered.view.frame = CGRectMake(0, 44, self.view.frame.size.width, WySCREENHEIGHT - NavViewHeight - 30 - 44);
    Delivered.view.frame = CGRectMake(0, 44, WySCREENWIDTH, WySCREENHEIGHT - NavViewHeight - 44  - SafeDistanceHeight);
//    NSLog(@"%f",NavViewHeight);

    [self addChildViewController:Delivered];
    
    
    
    
    _WySegmentedControl = [SGSegmentedControl segmentedControlWithFrame:CGRectMake(BiLiWidth(0), BiLiWidth(0), self.view.frame.size.width, 44) delegate:self segmentedControlType:SGSegmentedControlTypeStatic titleArr:_WyArray];
    [self.view addSubview:_WySegmentedControl];
    _WySegmentedControl.titleColorStateSelected = UIColorFromRGB(0xFADB14);
    _WySegmentedControl.indicatorColor = UIColorFromRGB(0xFADB14);
    _WySegmentedControl.backgroundColor = [UIColor whiteColor];
//    WyViewBorder(_WySegmentedControl, UIColorFromRGB(0x1896FF), 1);
    
   
   
    
    
    
}

- (void)SGSegmentedControl:(SGSegmentedControl *)segmentedControl didSelectBtnAtIndex:(NSInteger)index {
    CGFloat X = index * self.view.frame.size.width;
    
    self.WyScrollView.contentOffset = CGPointMake(X, 0);
    
    
    [self showVc:index];
}
-(void)iniSetChild
{
    
    BiographyVc  *Biography = [[BiographyVc alloc]init];
    [self addChildViewController:Biography];
      
    CelebrityinkVc  *Celebrityink = [[CelebrityinkVc alloc]init];
    [self addChildViewController:Celebrityink];

    channelVc  *channel = [[channelVc alloc]init];
    [self addChildViewController:channel];
    
    TopVc  *Completed = [[TopVc alloc]init];
    [self addChildViewController:Completed];
  
    
}
- (void)showVc:(NSInteger)index {
    
    CGFloat X = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    
    if (vc.isViewLoaded)
        return;
    
    [self.WyScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(X, 44, self.view.frame.size.width, WySCREENHEIGHT - NavViewHeight - 30 );
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    [self showVc:index];
    
    [self.WySegmentedControl titleBtnSelectedWithScrollView:scrollView];
}

//-(UITableView *)WyHomeTableView
//
//{
//
//    if (!_WyHomeTableView) {
//
//
//        _WyHomeTableView = [[UITableView alloc]init];
//           _WyHomeTableView.delegate = self;
//           _WyHomeTableView.dataSource = self;
//           _WyHomeTableView.showsVerticalScrollIndicator = YES;
//           _WyHomeTableView.backgroundColor = [UIColor whiteColor];
//           _WyHomeTableView.tableHeaderView = self.WyHeaderView;
//           _WyHomeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//           _WyHomeTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(Shuaxin)];
//           [self.view addSubview:_WyHomeTableView];
////         [_WyHomeTableView registerClass:[HomeViewCell class] forCellReuseIdentifier:@"Cell"];
//           [_WyHomeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//               make.top.bottom.left.right.mas_offset(0);
//           }];
//           [_WyHomeTableView.mj_header beginRefreshing];
//        if (@available(iOS 11.0, *)) {
//
//        _WyHomeTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        }
//    }
//    return _WyHomeTableView;
//
//}
//-(void)Shuaxin
//{
//    [self.WyHomeTableView reloadData];
//    [self.WyHomeTableView.mj_header endRefreshing];
//
//}
//-(UIView *)WyHeaderView
//
//{
//
//    UIView *WyHeaderView = [[UIView alloc]init];
//    [self.view addSubview:WyHeaderView];
//    WyHeaderView.backgroundColor = [UIColor whiteColor];
//    [WyHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.equalTo(CGSizeMake(WySCREENWIDTH, BiLiWidth(490)));
//        make.top.mas_offset(0);
//        make.centerX.mas_offset(0);
//    }];
//
//
//        NSMutableArray *  BannerViewArray = [NSMutableArray arrayWithObjects:@"home_banner1",@"home_banner2",@"home_banner3", nil];
//
//       _WyHomeSLBannerView = [[SLBannerView alloc]init];
//       _WyHomeSLBannerView.delegate = self;
//       [WyHeaderView addSubview:_WyHomeSLBannerView];
//       _WyHomeSLBannerView.durTimeInterval = 0.5;
//       _WyHomeSLBannerView.layer.cornerRadius = 10;
//       _WyHomeSLBannerView.clipsToBounds = YES;
//       _WyHomeSLBannerView.imgStayTimeInterval = 3;
//       _WyHomeSLBannerView.slImages = BannerViewArray;
//       [_WyHomeSLBannerView mas_makeConstraints:^(MASConstraintMaker *make) {
//           make.size.equalTo(CGSizeMake(WySCREENWIDTH, BiLiWidth(246)));
//           make.top.mas_offset(BiLiWidth(0));
//           make.centerX.mas_offset(0);
//       }];
//
//
//    NSArray *ImgArray = @[@"线装古籍",@"人物传记",@"名人墨迹"];
//
//    for (int i = 0; i<self.LabeArray.count; i++) {
//          UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//          [WyHeaderView addSubview:btn];
//          btn.tag = i;
//          [btn setTitle:self.LabeArray[i] forState:0];
//          [btn setFont:WySYSTEMFONT(14)];
//          [btn setImage:[UIImage imageNamed:ImgArray[i]] forState:0];
//          [btn setImageEdgeInsets:UIEdgeInsetsMake(-btn.titleLabel.intrinsicContentSize.height, 0, 0, -btn.titleLabel.intrinsicContentSize.width)];
//          [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.currentImage.size.height+20, -btn.currentImage.size.width, 0, 0)];
//          [btn setTitleColor:[UIColor blackColor] forState:0];
//          [btn addTarget:self action:@selector(selector:) forControlEvents:UIControlEventTouchUpInside];
//          [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//              make.size.equalTo(CGSizeMake(BiLiWidth(60), BiLiWidth(50)));
//              make.top.equalTo(self.WyHomeSLBannerView.bottom).offset(BiLiWidth(17));
//              make.left.mas_offset(BiLiWidth(35)+(BiLiWidth(120))*i);
//          }];
//      }
//
//
//    NSArray *BtnArray = @[@"新书频道",@"好书排行"];
//    for (int i = 0; i<BtnArray.count; i++) {
//        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [WyHeaderView addSubview:btn];
//        btn.tag = i;
//        [btn setBackgroundImage:[UIImage imageNamed:BtnArray[i]] forState:0];
//        [btn addTarget:self action:@selector(Classselector:) forControlEvents:UIControlEventTouchUpInside];
//        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.equalTo(CGSizeMake((WySCREENWIDTH-BiLiWidth(20))/2, BiLiWidth(90)));
//            make.top.equalTo(self.WyHomeSLBannerView.bottom).offset(BiLiWidth(98));
//            make.left.mas_offset(BiLiWidth(5)+(WySCREENWIDTH-BiLiWidth(20)+BiLiWidth(20))/2*i);
//        }];
//    }
//
//    self.BuyLabe = [UILabel labelWithContent:@"大家购买" superView:WyHeaderView textColor:WyBlackColor font:WySYSTEMFONT(16) textAlignment:NSTextAlignmentLeft];
//    [self.BuyLabe mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.equalTo(CGSizeMake(BiLiWidth(100), BiLiWidth(22)));
//        make.left.mas_offset(BiLiWidth(15));
//        make.bottom.mas_offset(BiLiWidth(-20));
//    }];
//
//
//
//
//
//
//    return WyHeaderView;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//
//{
//    return self.LabeBtnArray.count;
//}
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
//{
//
//    NSString *CellId = [NSString stringWithFormat:@"Cell%ld",(long)indexPath.row];
//
//
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
//
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId];
//    }
//    cell.backgroundColor = WyWhiteColor;
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    NSArray *imgArray = @[@"傅雷家书",@"平凡的世界：全三册"];
//    NSArray *imgMesArray = @[@"《傅雷家书》（新课标本）是傅雷之子傅敏先",@"这是一部现实主义小说，也是小说化的家族史"];
//    NSArray *imgHeadArray = @[@"椭圆形",@"椭圆形(1)"];
//    NSArray *userNameArray = @[@"时光荏苒又一年",@"匆匆别去又相逢"];
//    NSArray *fuleiArray = @[@"傅雷家书",@"傅雷",@"综合评分：4.8",@"¥29.50"];
//    NSArray *pingfanArray = @[@"平凡的世界：全三册",@"路遥",@"综合评分：4.8",@"¥45.50"];
//
//    UIImageView *BookImg = [UIImageView imageWithImgName:imgArray[indexPath.row] superView:cell];
//    [BookImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.equalTo(CGSizeMake(BiLiWidth(65), BiLiWidth(95)));
//        make.left.mas_offset(BiLiWidth(15));
//        make.top.mas_offset(BiLiWidth(10));
//    }];
//
//    UIImageView *BookMesImg = [UIImageView imageWithImgName:imgMesArray[indexPath.row] superView:cell];
//    [BookMesImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.equalTo(CGSizeMake(WySCREENWIDTH-BiLiWidth(30), BiLiWidth(80)));
//        make.centerX.mas_offset(BiLiWidth(0));
//        make.top.equalTo(BookImg.bottom).offset(BiLiWidth(3));
//    }];
//
//    UIImageView *imgHead = [UIImageView imageWithImgName:imgHeadArray[indexPath.row] superView:cell];
//    [imgHead mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.size.equalTo(CGSizeMake(BiLiWidth(30), BiLiWidth(30)));
//        make.left.mas_offset(BiLiWidth(15));
//        make.top.equalTo(BookMesImg.bottom).offset(BiLiWidth(10));
//    }];
//
//    UILabel *userName = [UILabel labelWithContent:userNameArray[indexPath.row] superView:cell textColor:WyBlackColor font:WySYSTEMFONT(14) textAlignment:NSTextAlignmentLeft];
//    [userName mas_makeConstraints:^(MASConstraintMaker *make) {
//         make.size.equalTo(CGSizeMake(BiLiWidth(200), BiLiWidth(20)));
//         make.left.equalTo(imgHead.right).offset(BiLiWidth(8));
//         make.top.equalTo(BookMesImg.bottom).offset(BiLiWidth(15));
//    }];
//
//
//
//    if (indexPath.row == 0) {
//
//        for (int i = 0; i<fuleiArray.count; i++) {
//            UILabel *labe = [UILabel labelWithContent:fuleiArray[i]superView:cell textColor:WyBlackColor font:WySYSTEMFONT(14) textAlignment:NSTextAlignmentLeft];
//            [labe mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.size.equalTo(CGSizeMake(BiLiWidth(200), BiLiWidth(20)));
//                make.left.equalTo(BookImg.right).offset(BiLiWidth(10));
//                make.top.mas_offset(BiLiWidth(10)+BiLiWidth(25)*i);
//            }];
//            labe.tag = i;
//            if (labe.tag == 1) {
//                labe.textColor = UIColorFromRGB(0x999999);
//            }else if (labe.tag == 2)
//            {
//                labe.textColor = UIColorFromRGB(0xFADB14);
//            }
//            else if (labe.tag == 3)
//            {
//                labe.textColor = UIColorFromRGB(0xFF4949);
//            }
//        }
//
//    }
//    else
//    {
//
//        for (int i = 0; i<pingfanArray.count; i++) {
//            UILabel *labe = [UILabel labelWithContent:pingfanArray[i]superView:cell textColor:WyBlackColor font:WySYSTEMFONT(14) textAlignment:NSTextAlignmentLeft];
//            [labe mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.size.equalTo(CGSizeMake(BiLiWidth(200), BiLiWidth(20)));
//                make.left.equalTo(BookImg.right).offset(BiLiWidth(10));
//                make.top.mas_offset(BiLiWidth(10)+BiLiWidth(25)*i);
//            }];
//            labe.tag = i;
//            if (labe.tag == 1) {
//                labe.textColor = UIColorFromRGB(0x999999);
//            }else if (labe.tag == 2)
//            {
//                labe.textColor = UIColorFromRGB(0xFADB14);
//            }
//            else if (labe.tag == 3)
//            {
//                labe.textColor = UIColorFromRGB(0xFF4949);
//            }
//        }
//
//    }
//
//
//    return cell;
//}
//
//
//
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//
//{
//    [[AlertControllerCustom shareInstance] showAlert:@"已购买！\n请查看其他类别详情" viewController:self];
//}
//
//-(void)selector:(UIButton *)button{
//
//    ClassViewController *calss = [[ClassViewController alloc]init];
//    calss.navStr = self.LabeArray[button.tag];
//    calss.type = (int)button.tag;
//    [self.navigationController pushViewController:calss animated:YES];
//}
//
//-(void)Classselector:(UIButton *)class
//{
//    ClassViewController *calss = [[ClassViewController alloc]init];
//    calss.navStr = self.LabeBtnArray[class.tag];
//    calss.type = (int)class.tag+3;
//    [self.navigationController pushViewController:calss animated:YES];
//}
//
//
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return BiLiWidth(220);
//}
//
//-(NSArray *)LabeArray
//
//{
//    if (!_LabeArray) {
//
//
//       _LabeArray = @[@"线装古籍",@"人物传记",@"名人墨迹"];
//
//    }
//
//    return _LabeArray;
//}
//-(NSArray *)LabeBtnArray
//
//{
//    if (!_LabeBtnArray) {
//
//
//        _LabeBtnArray = @[@"新书频道",@"好书排行"];
//
//    }
//
//    return _LabeBtnArray;
//}
//- (UILabel *)BuyLabe
//
//{
//    if (!_BuyLabe) {
//        _BuyLabe = [[UILabel alloc]init];
//    }
//    return _BuyLabe;
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
